package main.Q901_1000;

import java.util.Arrays;

public class Q901_910 {
	public static void main(String[] args) {
		System.out.println("Question901：");
		System.out.println("Question902：");
		System.out.println("Question903：");
		System.out.println("Question904：水果成篮");
		System.out.println("Question905：按奇偶排序数组");
		System.out.println("Question906：");
		System.out.println("Question907：");
		System.out.println("Question908：");
		System.out.println("Question909：");
		System.out.println("Question910：最小差值Ⅱ");
	}
}

class Question904 {
	public int totalFruit(int[] fruits) {
		int left = 0, right = 0, maxLength = 0, ln = fruits[left], rn = fruits[right];
		while (right < fruits.length) {
			if (fruits[right] == ln || fruits[right] == rn) {
				maxLength = Math.max(maxLength, right - left + 1);
				right++;
			} else {
				left = right - 1;
				ln = fruits[left];
				while (left >= 1 && fruits[left - 1] == ln) left--;
				rn = fruits[right];
				maxLength = Math.max(maxLength, right - left + 1);
			}
		}
		return maxLength;
	}
}

class Question905 {
	public int[] sortArrayByParity(int[] nums) {
		int index = -1;
		for (int i = 0; i < nums.length; i++) {
			if (nums[i] % 2 == 0) {
				int temp = nums[i];
				index++;
				nums[i] = nums[index];
				nums[index] = temp;
			}
		}
		return nums;
	}
}

class Question910 {
	public int smallestRangeII(int[] nums, int k) {
		Arrays.sort(nums);
		int length = nums.length - 1, result = nums[length] - nums[0];
		for (int i = 1; i < nums.length; i++) {
			int min = Math.min(nums[0] + k, nums[i] - k);
			int max = Math.max(nums[length] - k, nums[i - 1] + k);
			result = Math.min(result, max - min);
		}
		return result;
	}
}